<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>观察者模式2</title>
</head>
<body>
<script>
    function PubSub(){
        this.handle={};
    }
   PubSub.prototype= {
       on: function (eventName, callback) {
           //将事件保存一起，保存为对象，称为订阅
           event[eventName] = callback;
       },
       off: function (eventName) {
           //退订
           event[eventName] && (delete event[eventName]);
       },
       emit: function (eventName) {
           //发布
           event[eventName] && event[eventName](arguments[1])
       }
   };
   var obj1=new PubSub();
   //var obj2=new PubSub();
   obj1.on("newspaper",function(){
       arguments[0] ?console.log(arguments[0] +"新一期报纸出来了"):console.log("无名氏报纸出来了");
   });
    obj1.on("news",function(){
        arguments[0] ?console.log(arguments[0] +"新一期新闻出来了"):console.log("无名氏新闻出来了");
    });
    //obj2.on("news",function(){
        //arguments[0] ?console.log(arguments[0] +"新一期新闻出来了"):console.log("无名氏新闻出来了");
    //});

    obj1.emit("newspaper","bob");
    //obj2.emit("newspaper","john");



</script>
</body>
</html>